package com.story.pr.function;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import com.story.pr.R;
import com.story.pr.StoryProject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class StoryDetails extends Activity implements OnClickListener {
	TextView short_title, title, description;
	ImageView imvDetail, imvHome, imvSearch;
	RatingBar rateDetail;
	Button btnDoc, btnTai;
	String short_title1, title1, des, cover;
	Integer rate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);

		short_title = (TextView) findViewById(R.id.txtTitleText);
		title = (TextView) findViewById(R.id.title_detail);
		description = (TextView) findViewById(R.id.description);
		imvDetail = (ImageView) findViewById(R.id.cover_detail);
		rateDetail = (RatingBar) findViewById(R.id.rating_bar_details);

		this.getIntent().getExtras();
		Bundle bundle = this.getIntent().getExtras();

		short_title1 = bundle.getString("short_title");
		title1 = bundle.getString("title");
		des = bundle.getString("description");
		rate = bundle.getInt("rate");
		cover = bundle.getString("cover");

		short_title.setText(short_title1);
		title.setText(title1);
		description.setText(des);
		rateDetail.setRating(rate);
		imvDetail.setImageBitmap(GetFile(cover));

		// nút quay về trang home
		imvHome = (ImageView) findViewById(R.id.home);
		imvHome.setOnClickListener(this);

		// nút tới trang Search
		imvSearch = (ImageView) findViewById(R.id.search);
		imvSearch.setOnClickListener(this);

		btnDoc = (Button) findViewById(R.id.btndoc);
		btnDoc.setOnClickListener(this);
		btnTai = (Button) findViewById(R.id.btntai);
		btnTai.setOnClickListener(this);

	}

	// //////////////////////////////////////////////////////
	// Phương thức xử lí ảnh
	// //////////////////////////////////////////////////////

	public static Bitmap GetFile(String sUrl) {
		InputStream in = null;
		Bitmap bitmap = null;
		HttpURLConnection cnn = null;

		try {
			in = OpenHttpConnection(sUrl);
			bitmap = BitmapFactory.decodeStream(in);
			in.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (cnn != null) {
				try {
					cnn.disconnect();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return bitmap;
	}

	private static InputStream OpenHttpConnection(String urlString)
			throws IOException {
		// TODO Auto-generated method stub
		InputStream in = null;
		int response = -1;
		URL url = new URL(urlString);
		URLConnection conn = url.openConnection();

		if (!(conn instanceof HttpURLConnection))
			throw new IOException("khong phai 1 ket noi http");

		try {

			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setRequestMethod("GET");
			httpConn.connect();
			response = httpConn.getResponseCode();
			if (response == HttpURLConnection.HTTP_OK) {
				in = httpConn.getInputStream();
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw new IOException("Loi ket noi");
		}

		return in;
	}

	// /////////////////////////////////////////////////
	// Xử lý sự kiện Onclick
	// ////////////////////////////////////////////////

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search:
			searchStory();
			break;
		case R.id.home:
			backHome();
			break;
		case R.id.btndoc:
			readingView();
			break;
		case R.id.btntai:
			gotoDown();
			break;
		}
	}

	// Phương thức chuyển sang trang Tìm kiếm
	public void searchStory() {
		Intent i = new Intent();
		i.setAction("Search");
		i.setClass(this, StorySearch.class);
		startActivityForResult(i, 0);
	}

	// Phương thức chuyển sang trang đọc truyện

	public void readingView() {
		Bundle sendBundle = new Bundle();
		sendBundle.putString("title", title.getText().toString());
		Intent i = new Intent(StoryDetails.this, StoryRead.class);
		i.putExtras(sendBundle);
		startActivity(i);
	}

	// Phương thức quay về trang Home

	public void backHome() {
		Intent intent = new Intent(getApplicationContext(), StoryProject.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	// Phương thức chuyển sang trang Down
	public void gotoDown() {

	}

}
